<?php

namespace Controller\Upload;

class SwfImage extends \Controller\Base {

	public function actionIndex() {
		//上传配置
		$config = array(
			'savePath' => 'upload/' ,             //存储文件夹
			'maxSize' => 102400 ,                   //允许的文件最大尺寸，单位KB
			'allowFiles' => array('.gif' , '.png' , '.jpg' , '.jpeg')  //允许的文件格式
		);
		//上传文件目录
		$path = COMMON_DIR . '/uploads/';

		//背景保存在临时目录中
		$config['savePath' ] = $path;
		$up = new \Util\Uploader('Filedata', $config);
		$info = $up->getFileInfo();
		$name = str_replace($path, '', $info['url']);
		$this->cutImage($info['url'], 800);
		$this->cutImage($info['url'], 400);
		$this->cutImage($info['url'], 150);
		$encodeName = urldecode($name);
		/**
		 * 返回数据
		 */
		//$url = \Entity\Domain::instance()->www . '/File/GetImg?file=' . $name;
		echo $encodeName;exit;
	}

	private function cutImage($file, $width) {
		$size = getimagesize($file);
		$pathInfo = pathinfo($file);
		switch (strtolower($pathInfo['extension'])) {
			case 'png':
				$fn = 'imagepng';
				$iFn = 'imagecreatefrompng';
				break;
			case 'gif':
				$fn = 'imagegif';
				$iFn = 'imagecreatefromgif';
				break;
			case 'jpg':
			case 'jpeg':
				$fn = 'imagejpeg';
				$iFn = 'imagecreatefromjpeg';
			break;
			default:
				$this->throwException('图片格式错误');
		}
		$height = $width / $size[0] * $size[1];
		$img = $iFn($file);
		$dst = imagecreatetruecolor($width, $height);
		imagecopyresampled($dst, $img , 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
		$nowFile = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '_' . $width . '.' . $pathInfo['extension'];
		if ($fn == 'imagejpeg') {
			$fn($dst, $nowFile, 100);
		} else {
			$fn($dst, $nowFile);
		}
		return $file;
	}
}